/*

main.js

Copyright (C) 2012 Fraunhofer Institute for Secure Information Technology

Authors: Oliver Hoffmann <oliver.hoffmann@sit.fraunhofer.de>
         Sebastian Ruhleder <sebastian.ruhleder@sit.fraunhofer.de>
         
Contact: Andreas Poller <andreas.poller@sit.fraunhofer.de>
         Fraunhofer Institute for Secure Information Technology
         Rheinstraße 75
         64295 Darmstadt
         Germany

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.

*/

(function() {
  var ROSEExtension, StorageEngine, createUserId, extension, root;

  root = typeof exports !== "undefined" && exports !== null ? exports : this;

  ROSEExtension = (function() {

    function ROSEExtension() {
      var _this = this;
      if (!kango.storage.getItem('rose-first-run')) {
        kango.storage.setItem('rose-first-run', new Date());
      }
      if (!kango.storage.getItem('rose-userid')) {
        kango.storage.setItem('rose-userid', createUserId());
      }
      if (!kango.storage.getItem('rose-settings')) {
        kango.storage.setItem('rose-settings', {
          reminder: true,
          language: "browser",
          filterEvents: true
        });
      }
      // SET ROSE DATA VERSION
      if (!kango.storage.getItem('rose-data-version')) {
        kango.storage.setItem('rose-data-version', 1.2);
      }
      if (!kango.storage.getItem('roseData')) {
        var initialData = {
          'interactions': [],
          'roseComments': {},
          'heartbeat': ""
        };
        kango.storage.setItem('roseData', initialData);
      }
    }

    return ROSEExtension;

  })();

  StorageEngine = (function() {

    function StorageEngine() {
      this._initStorage();
    }

    StorageEngine.prototype.getCommentById = function(id) {
      var item, key, val, _i, _len, _ref;
      _ref = this._comments;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        item = _ref[_i];
        for (key in item) {
          val = item[key];
          if (key === id) {
            return val;
          }
        }
      }
    };

    StorageEngine.prototype.getComments = function() {
      return this._comments;
    };

    StorageEngine.prototype.getDiary = function() {
      return this._diary;
    };

    StorageEngine.prototype.clearComments = function() {
      this._comments = [];
      return this._flushStorage();
    };

    StorageEngine.prototype.clearInteractions = function() {
      this._interactions = [];
      return this._flushStorage();
    };

    StorageEngine.prototype.getInteractions = function() {
      return this._interactions;
    };

    StorageEngine.prototype.saveComment = function(comment) {
      this._comments.push(comment);
      return this._flushStorage();
    };

    StorageEngine.prototype.saveDiary = function(text) {
      this._diary = text;
      return this._flushStorage();
    };

    StorageEngine.prototype.saveInteraction = function(interaction) {
      kango.console.log(JSON.stringify(interaction));
      this._interactions.push(interaction);
      return this._flushStorage();
    };

    StorageEngine.prototype._initStorage = function() {
      var _ref, _ref1, _ref2;
      this._comments = (_ref = kango.storage.getItem('rose-comments')) !== null ? _ref : [];
      this._diary = (_ref1 = kango.storage.getItem('rose-diary')) !== null ? _ref1 : [];
      return this._interactions = (_ref2 = kango.storage.getItem('rose-interactions')) != null ? _ref2 : [];
    };

    StorageEngine.prototype._flushStorage = function() {
      kango.storage.setItem('rose-comments', this._comments);
      kango.storage.setItem('rose-diary', this._diary);
      kango.storage.setItem('rose-interactions', this._interactions);
      kango.console.log('Flushed data.');
      return true;
    };

    return StorageEngine;

  })();

  createUserId = function() {
    var char_set, i, text, _i;
    text = "";
    char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (i = _i = 0; _i <= 7; i = ++_i) {
      text += char_set.charAt(Math.floor(Math.random() * char_set.length));
    }
    return text;
  };

  extension = new ROSEExtension();

  root.storageEngine = new StorageEngine();

}).call(this);